#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2020/12/23 9:25
# @Author  : CHEN Wang
# @Site    : 
# @File    : multi_period_attribution.py
# @Software: PyCharm 

"""
脚本说明: 多期收益率归因
"""

import numpy as np


def absolute_multi_period(ret_series):
    total_return = ((1 + ret_series).cumprod() - 1)[-1]
    K = np.log(1+total_return) / total_return
    k_t = np.log((1+ret_series).astype('float')) / ret_series

    coefficient = k_t / K
    coefficient.rename('multi_period_coff', inplace=True)
    return coefficient


def relative_multi_period(port_ret_series, benchmark_ret_series):

    port_total_return = ((1 + port_ret_series).cumprod() - 1)[-1]
    benchmark_total_return = ((1 + benchmark_ret_series).cumprod() - 1)[-1]
    total_active_return = port_total_return - benchmark_total_return
    K = (np.log(1 + port_total_return) - np.log(1 + benchmark_total_return)) / total_active_return

    active_ret_series = port_ret_series - benchmark_ret_series

    k_t = (np.log(1 + port_ret_series) - np.log(1 + benchmark_ret_series)) / active_ret_series

    coefficient = k_t / K
    coefficient.rename('multi_period_coff', inplace=True)
    return coefficient


if __name__ == '__main__':
    pass